package com.example.weight.householdWaste;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.greenSeasons.compost.CompostMonthInspectorActivity;
import com.example.weight.greenSeasons.compost.CompostMonthMaintenanceActivity;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class HouseholdWasteMaintainNavActivity extends AppCompatActivity {

    private String name, role, factory;

    private Button yeyabujin, jinliaoluoxuanbianpin, tishengluoxuan,
            podaijijinliaobanlian, podaijiduanliaoshoujiluoxuan, podaijichuliaobanlian,
            shenghuolajifenlijiduanliaoluoxuan, guntongshai, guntongshaishaishangwushusongluoxuan,
            pingleifenlijijinliaoluoxuan, pingleifenliji, kefuwujinliaocixuanpidaiji,
            pingleifenjianbanlian, pingleifenlijichuliaoluoxuan, keranwushusongluoxuan,
            shenghuolajifenlijichangliaoshusongluoxuan, shaixiawushusongluoxuan1, shaixiawushusongluoxuan2,
            kefuwufensuijichuliaoluoxuan, kefuwufensuiji1, kefuwufensuiji2, fengxuanfensuiji,
            suliaofenlijijinliaobanlian, cixuanpidaiji, keranwuchucunxiangjinliaochengzhongpidai,
            kefuwuchucunxiangjinliaochengzhongpidai, jiankongxitong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_household_waste_maintain_nav);

        Intent previousIntent = getIntent();
        name = previousIntent.getStringExtra("name");
        role = previousIntent.getStringExtra("role");
        factory = previousIntent.getStringExtra("factory");

        TitleBar titleBar = findViewById(R.id.hhw_maintain_plan_nav_titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {}

            @Override
            public void onRightClick(View v) {}
        });

        yeyabujin = findViewById(R.id.hhw_yeyabujin);
        jinliaoluoxuanbianpin = findViewById(R.id.hhw_jinliaoluoxuanbianpin);
        tishengluoxuan = findViewById(R.id.hhw_tishengluoxuan);
        podaijijinliaobanlian = findViewById(R.id.hhw_podaijijinliaobanlian);
        podaijiduanliaoshoujiluoxuan = findViewById(R.id.hhw_podaijiduanliaoshoujiluoxuan);
        podaijichuliaobanlian = findViewById(R.id.hhw_podaijichuliaobanlian);
        shenghuolajifenlijiduanliaoluoxuan = findViewById(R.id.hhw_shenghuolajifenlijiduanliaoluoxuan);
        guntongshai = findViewById(R.id.hhw_guntongshai);
        guntongshaishaishangwushusongluoxuan = findViewById(R.id.hhw_guntongshaishaishangwushusongluoxuan);
        pingleifenlijijinliaoluoxuan = findViewById(R.id.hhw_pingleifenlijijinliaoluoxuan);
        pingleifenliji = findViewById(R.id.hhw_pingleifenliji);
        kefuwujinliaocixuanpidaiji = findViewById(R.id.hhw_kefuwujinliaocixuanpidaiji);
        pingleifenjianbanlian = findViewById(R.id.hhw_pingleifenjianbanlian);
        pingleifenlijichuliaoluoxuan = findViewById(R.id.hhw_pingleifenlijichuliaoluoxuan);
        keranwushusongluoxuan = findViewById(R.id.hhw_keranwushusongluoxuan);
        shenghuolajifenlijichangliaoshusongluoxuan = findViewById(R.id.hhw_shenghuolajifenlijichangliaoshusongluoxuan);
        shaixiawushusongluoxuan1 = findViewById(R.id.hhw_shaixiawushusongluoxuan1);
        shaixiawushusongluoxuan2 = findViewById(R.id.hhw_shaixiawushusongluoxuan2);
        kefuwufensuijichuliaoluoxuan = findViewById(R.id.hhw_kefuwufensuijichuliaoluoxuan);
        kefuwufensuiji1 = findViewById(R.id.hhw_kefuwufensuiji1);
        kefuwufensuiji2 = findViewById(R.id.hhw_kefuwufensuiji2);
        fengxuanfensuiji = findViewById(R.id.hhw_fengxuanfensuiji);
        suliaofenlijijinliaobanlian = findViewById(R.id.hhw_suliaofenlijijinliaobanlian);
        cixuanpidaiji = findViewById(R.id.hhw_cixuanpidaiji);
        keranwuchucunxiangjinliaochengzhongpidai = findViewById(R.id.hhw_keranwuchucunxiangjinliaochengzhongpidai);
        kefuwuchucunxiangjinliaochengzhongpidai = findViewById(R.id.hhw_kefuwuchucunxiangjinliaochengzhongpidai);
        jiankongxitong = findViewById(R.id.hhw_jiankongxitong);

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/shlj/equipmentMaintenance/is_lastweek_complete";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (!jsonObject.isNull("data")) {
                            JSONArray data = jsonObject.getJSONArray("data");
                            int checkedColor = Color.parseColor("#66CC66");
                            int halfCheckedColor = Color.parseColor("#FF9966");
                            int i = 0;
                            if (data.getInt(i++) == 2)
                                yeyabujin.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                yeyabujin.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                jinliaoluoxuanbianpin.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                jinliaoluoxuanbianpin.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                tishengluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                tishengluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                podaijijinliaobanlian.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                podaijijinliaobanlian.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                podaijiduanliaoshoujiluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                podaijiduanliaoshoujiluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                podaijichuliaobanlian.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                podaijichuliaobanlian.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                shenghuolajifenlijiduanliaoluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                shenghuolajifenlijiduanliaoluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                guntongshai.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                guntongshai.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                guntongshaishaishangwushusongluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                guntongshaishaishangwushusongluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                pingleifenlijijinliaoluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                pingleifenlijijinliaoluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                pingleifenliji.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                pingleifenliji.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                kefuwujinliaocixuanpidaiji.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                kefuwujinliaocixuanpidaiji.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                pingleifenjianbanlian.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                pingleifenjianbanlian.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                pingleifenlijichuliaoluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                pingleifenlijichuliaoluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                keranwushusongluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                keranwushusongluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                shenghuolajifenlijichangliaoshusongluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                shenghuolajifenlijichangliaoshusongluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                shaixiawushusongluoxuan1.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                shaixiawushusongluoxuan1.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                shaixiawushusongluoxuan2.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                shaixiawushusongluoxuan2.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                kefuwufensuijichuliaoluoxuan.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                kefuwufensuijichuliaoluoxuan.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                kefuwufensuiji1.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                kefuwufensuiji1.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                kefuwufensuiji2.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                kefuwufensuiji2.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                fengxuanfensuiji.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                fengxuanfensuiji.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                suliaofenlijijinliaobanlian.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                suliaofenlijijinliaobanlian.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                cixuanpidaiji.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                cixuanpidaiji.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                keranwuchucunxiangjinliaochengzhongpidai.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                keranwuchucunxiangjinliaochengzhongpidai.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i++) == 2)
                                kefuwuchucunxiangjinliaochengzhongpidai.setBackgroundColor(checkedColor);
                            else if (data.getInt(i-1) == 1)
                                kefuwuchucunxiangjinliaochengzhongpidai.setBackgroundColor(halfCheckedColor);
                            if (data.getInt(i) == 2)
                                jiankongxitong.setBackgroundColor(checkedColor);
                            else if (data.getInt(i) == 1)
                                jiankongxitong.setBackgroundColor(halfCheckedColor);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }, error -> Toast.makeText(HouseholdWasteMaintainNavActivity.this,
                        "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<>();
                map.put("equipmentsName", "液压步进式给料机|进料螺旋(变频)|提升螺旋|破袋机进料板链|破袋机短料收集螺旋|" +
                        "破袋机出料板链|生活垃圾分离机短料螺旋|滚筒筛|滚筒筛筛上物输送螺旋|瓶类分离机进料螺旋|" +
                        "瓶类分离机|可腐物进料磁选皮带机|瓶类分拣板链|瓶类分离机出料螺旋|可燃物输送螺旋|" +
                        "生活垃圾分离机长料输送螺旋|筛下物输送螺旋-1|筛下物输送螺旋-2|可腐物粉碎机出料螺旋|" +
                        "可腐物粉碎机-1|可腐物粉碎机-2|风选粉碎机|塑料分离机进料板链|磁选皮带机|" +
                        "可燃物储存箱进料称重皮带|可腐物储存箱进料称重皮带|监控系统");
                map.put("menstanceNum", "2|2|2|3|2|2|2|2|2|2|3|2|3|2|2|1|2|2|2|2|2|2|3|2|2|2|1");
                map.put("role", role.equals("操作人")?"operator":"examiner");
                map.put("factory", factory);
                return map;
            }
        };
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//        StringRequest stringRequest27 = new StringRequest(Request.Method.POST, url, response -> {
//            try {
//                JSONObject jsonObject = new JSONObject(response);
//                if (!jsonObject.isNull("data")) {
//                    String data = jsonObject.getString("data");
//                    if (data == 2)
//                        jiankongxitong.setBackgroundColor(checkedColor);
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }, error -> Toast.makeText(HouseholdWasteMaintainNavActivity.this,
//                "获取信息失败 " + error.getMessage(), Toast.LENGTH_SHORT).show()) {
//            @Override
//            protected Map<String, String> getParams() {
//                Map<String, String> map = new HashMap<>();
//                map.put("equipmentName", "监控系统");
//                map.put("menstanceNum", "1");
//                return map;
//            }
//        };
//        stringRequest27.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
//                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(stringRequest);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            boolean needRefresh = data.getBooleanExtra("needRefresh", true);
            if (needRefresh)
                refresh();
        }
    }

    private void go(String fName) {
        Intent intent;
//        intent =new Intent(HouseholdWasteMaintainNavActivity.this,
//                HouseholdWasteMaintainActivity.class);
        if (role.equals("操作人"))
            intent = new Intent(this, FacilityMaintenanceActivity.class);
        else
            intent = new Intent(this, FacilityMaintanenceInspectorActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("fName", fName);
        intent.putExtra("factory", factory);
        startActivityForResult(intent, 1);
    }

    public void yeyabujinshigeiliaoji(View view) {
        go("液压步进式给料机");
    }
    public void jinliaoluoxuan(View view) {
        go("进料螺旋(变频)");
    }
    public void tishengluoxuan(View view) {
        go("提升螺旋");
    }
    public void podaijijinliaobanlian(View view) {
        go("破袋机进料板链");
    }
    public void podaijiduanliaoshoujiluoxuan(View view) {
        go("破袋机短料收集螺旋");
    }
    public void podaijichuliaobanlian(View view) {
        go("破袋机出料板链");
    }
    public void shenghuolajifenlijiduanliaoluoxuan(View view) {
        go("生活垃圾分离机短料螺旋");
    }
    public void guntongshai(View view) {
        go("滚筒筛");
    }
    public void guntongshaishangwushusongluoxuan(View view) {
        go("滚筒筛筛上物输送螺旋");
    }
    public void pingfenlijijinliaoluoxuan(View view) {
        go("瓶类分离机进料螺旋");
    }
    public void pingleifenliji(View view) {
        go("瓶类分离机");
    }
    public void kefuwujinliaocixuanpidaiji(View view) {
        go("可腐物进料磁选皮带机");
    }
    public void pingleifenjianbanlian(View view) {
        go("瓶类分拣板链");
    }
    public void pingleifenlijichuliaoluoxuan(View view) {
        go("瓶类分离机出料螺旋");
    }
    public void keranwushusongluoxuan(View view) {
        go("可燃物输送螺旋");
    }
    public void shenghuolajifenlijichangliaoshusongluoxuan(View view) {
        go("生活垃圾分离机长料输送螺旋");
    }
    public void shaixiawushusongluoxuan_1(View view) {
        go("筛下物输送螺旋-1");
    }
    public void shaixiawushusongluoxuan_2(View view) {
        go("筛下物输送螺旋-2");
    }
    public void kefuwufensuijichuliaoluoxuan(View view) {
        go("可腐物粉碎机出料螺旋");
    }
    public void kefuwufensuiji_1(View view) {
        go("可腐物粉碎机-1");
    }
    public void kefuwufensuiji_2(View view) {
        go("可腐物粉碎机-2");
    }
    public void fengxuanfensuiji(View view) {
        go("风选粉碎机");
    }
    public void suliaofenlijijinliaobanlian(View view) {
        go("塑料分离机进料板链");
    }
    public void keranwuchucunxiangjinliaochengzhongpidai(View view) {
        go("可燃物储存箱进料称重皮带");
    }
    public void kefuwuchucunxiangjinliaochengzhongpidai(View view) {
        go("可腐物储存箱进料称重皮带");
    }
    public void cixuanpidaiji(View view) {
        go("磁选皮带机");
    }
    public void jiankongxitong(View view) {
        go("监控系统");
    }

    // 刷新activity
    public void refresh() {
        finish();
        Intent intent = new Intent(HouseholdWasteMaintainNavActivity.this,
                HouseholdWasteMaintainNavActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("role", role);
        intent.putExtra("factory", factory);
        startActivity(intent);
    }
}